//
//  UIViewController+Extension.swift
//  mmda
//
//  Created by 李陆涛 on 2018/8/9.
//  Copyright © 2018年 mmda. All rights reserved.
//

import UIKit

/// 扩展属性的key
private struct AssociatedKeys {
    
    static var kLastBGColor = "kLasetBGColor"
    static var kLastNavColor = "kLastNavColor"
    static var kAlpha = "kAlpha"
    
    static var kTransitionFlag = "kTransitionFlag"
    static var kTransitionType = "kTransitionType"
    static var kTransitionDelegate = "kTransitionDelegate"
}


// MARK: - 方法交换
extension UIViewController {
    
    
    static func aNotherswizzle() {

        swizzleMethod
    }
    
    private static let swizzleMethod: Void = {
        
        let originalSelector1 = #selector(dismiss(animated:completion:))
        let swizzledSelector1 = #selector(llt_dismiss(animated:completion:))
        
        swizzlingForClass(UIViewController.self, originalSelector: originalSelector1, swizzledSelector: swizzledSelector1)
        
        let originalSelector2 = #selector(viewDidLoad)
        let swizzledSelector2 = #selector(llt_viewDidLoad)
        
        swizzlingForClass(UIViewController.self, originalSelector: originalSelector2, swizzledSelector: swizzledSelector2)
    }()
    
    private static func swizzlingForClass(_ forClass: AnyClass, originalSelector: Selector, swizzledSelector: Selector) {
        
        guard let originalMethod = class_getInstanceMethod(forClass, originalSelector),
            let swizzledMethod = class_getInstanceMethod(forClass, swizzledSelector) else {
                return
        }
        
        if class_addMethod(forClass, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod)) {
            
            class_replaceMethod(forClass, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod))
        } else {
            
            method_exchangeImplementations(originalMethod, swizzledMethod)
        }
    }
    
    @objc private func llt_dismiss(animated flag: Bool, completion: (() -> Void)? = nil) {
        
        
        if self.llt_transitionFlag ?? false {
            
            self.transitioningDelegate = self.llt_transitionDelegate;
            
        }
        
        self.llt_dismiss(animated: flag, completion: completion)
        
    }
    
    @objc private func llt_viewDidLoad() {
        
        self.setCustomBarStyle(.black)
    }
}

enum CustomBarStyle {
    case black
    case white
    case alpha(CGFloat)
}


// MARK: - 配置导航条
extension UIViewController {
    
    
    func setCustomBarStyle(_ style:CustomBarStyle) {
        
        switch style {
            
        case .black:
            
            self.navBarBarTintColor = UIColor.black
            self.navBarTintColor = UIColor.white
            self.navBarTitleColor = UIColor.white
            self.statusBarStyle = .lightContent
            
        case .white:
            
            self.navBarBarTintColor = UIColor.white
            self.navBarTintColor = UIColor.black
            self.navBarTitleColor = UIColor.black
            self.statusBarStyle = .default
            
        case .alpha(let alpha):
            
            if self.navBarBackgroundAlpha == alpha {
                
                return
            }
            
            self.navBarBarTintColor = UIColor.black
            self.navBarTintColor = UIColor.white
            self.navBarTitleColor = UIColor.white
            self.statusBarStyle = .lightContent
            
            UIView.animate(withDuration: 0.25) {
                
                [weak self] in
                guard let `self` = self else { return }
                
                self.navBarBackgroundAlpha = alpha
            }
        }
    }

//    private var kLastBGColor: UIColor {
//        
//        get {
//            
//            return objc_getAssociatedObject(self, &AssociatedKeys.kLastBGColor) as? UIColor ?? .black
//        }
//        set {
//            
//            objc_setAssociatedObject(self, &AssociatedKeys.kLastBGColor, newValue,.OBJC_ASSOCIATION_COPY_NONATOMIC)
//        }
//    }
//    
//    private var kLastNavColor: UIColor {
//        
//        get {
//            
//            return objc_getAssociatedObject(self, &AssociatedKeys.kLastNavColor) as? UIColor ?? .white
//        }
//        set {
//            
//            objc_setAssociatedObject(self, &AssociatedKeys.kLastNavColor, newValue,.OBJC_ASSOCIATION_COPY_NONATOMIC)
//        }
//    }
//    
//    private var kLastAlpha: CGFloat {
//        
//        get {
//            
//            return objc_getAssociatedObject(self, &AssociatedKeys.kAlpha) as? CGFloat ?? 1.0
//        }
//        set {
//            
//            objc_setAssociatedObject(self, &AssociatedKeys.kAlpha, newValue,.OBJC_ASSOCIATION_COPY_NONATOMIC)
//        }
//    }
//
//    func setNavBackgroundColor(color:UIColor) {
//        
//        guard let nav = self.navigationController as? BaseNav else { return }
//        
//        nav.setNavBgColor(color: color)
//    }
//    
//    func setNavColor(color:UIColor) {
//        
//        guard let nav = self.navigationController as? BaseNav else { return }
//        
//        nav.setNavFrontColor(color: color)
//    }
//    
//    
//    func setNavAlpha(alpha:CGFloat) {
//        
//        guard let nav = self.navigationController as? BaseNav else { return }
//        
////        nav.bgColor = UIColor.clear
//        
//        nav.setNavBgAlpha(alpha: alpha)
//    }
}


// MARK: - 常用方法
extension UIViewController {
    
    func hasLogin() -> Bool {
        
        if let _ = AppManager.share.token {
            
            return true
        }
        
        
        let baseVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "LoginVC")
        
        self.present(baseVC, animated: true, completion: nil)
        
        return false
    }
    
    func showToastActivity(_ show:Bool) {
        
        if show {
            self.view.makeToastActivity(.center)
        } else {
            self.view.hideToastActivity()
        }
    }
    
    func showToast(_ msg:String?) {
        
        self.view.makeToast(msg)
        
    }
}


// MARK: - 转场动画
extension UIViewController {
    
    private var llt_transitionFlag: Bool? {
        
        get {
            
            let flag = objc_getAssociatedObject(self, &AssociatedKeys.kTransitionFlag)
            
            return flag as? Bool ?? false
        }
        set {
            
            objc_setAssociatedObject(self, &AssociatedKeys.kTransitionFlag, newValue,.OBJC_ASSOCIATION_COPY_NONATOMIC)
        }
    }
    
    
    private var llt_transitionType: LLTTransitionAnimationType? {
        
        get {
            
            return objc_getAssociatedObject(self, &AssociatedKeys.kTransitionType) as? LLTTransitionAnimationType ?? .sysFade
        }
        set {
            
            objc_setAssociatedObject(self, &AssociatedKeys.kTransitionType, newValue,.OBJC_ASSOCIATION_COPY_NONATOMIC)
        }
    }
    
    private var llt_transitionDelegate: UIViewControllerTransitioningDelegate? {
        
        get {
            
            return objc_getAssociatedObject(self, &AssociatedKeys.kTransitionDelegate) as? UIViewControllerTransitioningDelegate
        }
        set {
            
            objc_setAssociatedObject(self, &AssociatedKeys.kTransitionDelegate, newValue,.OBJC_ASSOCIATION_COPY_NONATOMIC)
        }
    }
    
    func llt_present(_ viewControllerToPresent: UIViewController, animated type: LLTTransitionAnimationType, completion: (() -> Void)? = nil) {
        
        viewControllerToPresent.llt_transitionFlag = true
        viewControllerToPresent.llt_transitionType = type
        viewControllerToPresent.transitioningDelegate = viewControllerToPresent
        
        self.present(viewControllerToPresent, animated: true, completion: completion)
    }
}


var transion:LLTTransition?


// MARK: - 转场动画代理
extension UIViewController : UIViewControllerTransitioningDelegate {
    
    public func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        if self.llt_transitionFlag == false {
            return nil
        }
        
        if transion == nil {
            
            transion = LLTTransition()
        }
        
        transion?.transitionType = .present
        transion?.animationType = self.llt_transitionType ?? .sysFade
        
        return transion
        
    }
    
    public func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        if self.llt_transitionFlag == false {
            return nil
        }
        
        if transion == nil {
            
            transion = LLTTransition()
        }
        
        transion?.transitionType = .dismiss
        transion?.animationType = self.llt_transitionType ?? .sysFade
        
        return transion
    }
}
